﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Db
{
	public class Statement
	{
		private string stmt = "";
		private List<SqlParameter> parameters = new List<SqlParameter>();
		private static String[,] DBTypeConversionKey = new String[,] 
		{
			{"bigint","Int64"},
			{"datetime","DateTime"},
            {"timestamp","DateTime"},
			{"decimal","Decimal"},
			{"float","Double"},
			{"int","Int32"},
            {"tinyint", "Byte"},
			{"smallint","Int16"},
			{"string","String"},
			{"varchar","String"}
		};

		public Statement(string stmt)
		{
			this.stmt = stmt;
		}

		public string Stmt
		{
			get { return stmt; }
			set { stmt = value; }
		}

		public List<SqlParameter> Parameters
		{
			get { return parameters; }
			set { parameters = value; }
		}

		public void AddParameter(string name, object value, string type)
		{
			SqlParameter par = new SqlParameter();

			par.ParameterName = name;
			par.Value = value;
			par.DbType = this.GetType(type);

			parameters.Add(par);
		}

		private DbType GetType(string t)
		{
			DbType type = (DbType)Enum.Parse(typeof(DbType), "String");
			string to = "";

            t = t.ToLower();

            //Console.WriteLine("Søger: " + t);
			for (int i = 0; i < DBTypeConversionKey.Length; i++)
			{
                //Console.WriteLine("Kigger på: " + DBTypeConversionKey[i, 0]);
                if (DBTypeConversionKey[i, 0].Equals(t))
                {
                    to = DBTypeConversionKey[i, 1];
                    break;
                }
			}

            //Console.WriteLine("To: " + to);

			if (to != null)
			{
				type = (DbType)Enum.Parse(typeof(DbType), to);
			}

			return type;
		}
	}
}
